﻿using System.Linq;
using TrueSync;

namespace HEFramework
{
    public static class SkillLockComponentSystem
    {
        [EntitySystem]
        public class SkillLockComponentAwakeSystem : AwakeSystem<SkillLockComponent>
        {
            protected override void Awake(SkillLockComponent _self)
            {
            }
        }

        [EntitySystem]
        public class SkillLockComponentUpdateSystem : UpdateSystem<SkillLockComponent>
        {
            protected override void Update(SkillLockComponent _self)
            {
                _self.Caches.Clear();
                for (int i = 0; i < _self.Datas.Count; i++)
                {
                    int id = _self.Datas[i];
                    if (_self.Ready(id) == (int)eSkillErrorCode.None)
                    {
                        _self.Caches.Add(id);
                    }
                }

                for (int i = 0; i < _self.Caches.Count; i++)
                {
                    int id = _self.Caches[i];
                    _self.UnLock(id);
                    _self.RemoveSkillLockData(id);
                }
            }
        }

        public static void AddSkillLockData(this SkillLockComponent _self, int _id)
        {
            if (_self.Datas.Contains(_id))
            {
                return;
            }

            if (_self.Ready(_id) == (int)eSkillErrorCode.UnLock)
            {
                _self.Datas.Add(_id);
            }
        }

        public static void RemoveSkillLockData(this SkillLockComponent _self, int _id)
        {
            if (!_self.Datas.Contains(_id))
            {
                return;
            }

            _self.Datas.Remove(_id);
        }

        public static int Ready(this SkillLockComponent _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var sc = _self.GetParent<SkillComponent>();
            if (dc == null || sc == null)
            {
                return (int)eSkillErrorCode.System;
            }

            var ac = sc.GetParentChild<AttributeComponent>();
            if (ac == null)
            {
                return (int)eSkillErrorCode.System;
            }

            var table = dc.LoadDataTable<DRSkill>();
            var data = table.GetData(_id);

            if (ac.LV < data.LockLV)
            {
                return (int)eSkillErrorCode.UnLock;
            }

            if (ac.RANK < data.LockRANK)
            {
                return (int)eSkillErrorCode.UnLock;
            }

            return (int)eSkillErrorCode.None;
        }

        private static void UnLock(this SkillLockComponent _self, int _id)
        {
            var sec = _self.GetParentChild<SkillExecutionComponent>();
            if (sec == null)
            {
                return;
            }

            sec.TriggerPassiveSkill(_id, eSkillTriggerType.OnAdd, _self.GetMainEntity(), _self.GetMainEntityPos());
        }

        public static Entity GetMainEntity(this SkillLockComponent _self)
        {
            return _self.Parent.Parent;
        }

        public static TSVector GetMainEntityPos(this SkillLockComponent _self)
        {
            var entity = _self.GetMainEntity();
            var tc = entity.GetChild<TransformComponent>();
            if (tc == null)
            {
                return new TSVector();
            }

            return tc.P;
        }
    }
}